package com.example.stmall.manager.config;

import com.example.stmall.manager.entity.Admin;
import com.example.stmall.manager.interceptor.AdminInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 图片的静态映射路径
 * 配置SpringMVC，以后有SpringMVC也在这里配置
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${upload.path}")
    private String path;
    @Autowired
    private AdminInterceptor adminInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        //添加上传图片的映射路径
        //当客户端访问/upload/路径下的任何资源时，将从本地资源提取
        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:"+path);
    }
    /**
     * 配置拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册管理员的拦截器
        registry.addInterceptor(adminInterceptor).addPathPatterns("/**")     //添加要拦截的路径
                                                 .excludePathPatterns("/layui/**","/admin/login.html","/vercode","/api/admin/login");    //排除不需要拦截的路径
    }
}
